package com.huabo.monitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbfk.util.JsonBean;
import com.hbfk.util.ResponseFormat;
import com.huabo.monitor.oracle.entity.TblTestTemplate;
import com.huabo.monitor.oracle.entity.TblTestplan;
import com.huabo.monitor.oracle.mapper.TblTestplanMapper;
import com.huabo.monitor.service.TblTestPlanService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class TblTestPlanServiceImpl implements TblTestPlanService {

    @Resource
    private TblTestplanMapper tblTestplanMapper;

    @Override
    public JsonBean save(TblTestplan tblTestplan) {
        if (tblTestplan == null){
            return ResponseFormat.retParam(0,10002,null);
        }
        tblTestplanMapper.insert(tblTestplan);
        return ResponseFormat.retParam(1,200,null);
    }

    @Override
    public JsonBean add(TblTestplan tblTestplan) {
        if (tblTestplan == null){
            return ResponseFormat.retParam(0,10002,null);
        }
        tblTestplanMapper.insert(tblTestplan);
        return ResponseFormat.retParam(1,200,null);
    }

    @Override
    public JsonBean deleteById(BigDecimal testplanid) {
        if (testplanid==null){
            return ResponseFormat.retParam(0,10002,null);
        }
        tblTestplanMapper.deleteById(testplanid);
        return ResponseFormat.retParam(1,200,null);
    }

    @Override
    public JsonBean selectList(String plannumber, String planname, String planstatus, Date starttime_min, Date starttime_max,Integer pageNumber,Integer pageSize) {
        return null;
    }

    @Override
    public JsonBean updateById(BigDecimal testplanid) {
        if (testplanid==null){
            return ResponseFormat.retParam(0,10002,null);
        }
        tblTestplanMapper.updateById(testplanid);
        return ResponseFormat.retParam(1,200,null);
    }

    @Override
    public JsonBean findById(BigDecimal testplanid) {
        if (testplanid==null){
            return ResponseFormat.retParam(0,10002,null);
        }
        tblTestplanMapper.findById(testplanid);
        return ResponseFormat.retParam(1,200,null);
    }

    @Override
    public JsonBean update(BigDecimal testplanid) {
        if (testplanid==null){
            return ResponseFormat.retParam(0,10002,null);
        }
        tblTestplanMapper.updateById(testplanid);
        return ResponseFormat.retParam(1,200,null);
    }

    @Override
    public JsonBean saveAll(List<String> plans) {
        for (String plan : plans) {
            if (plan==null){
                return ResponseFormat.retParam(0,10002,null);
            }
            tblTestplanMapper.insert(plan);
        }
        return ResponseFormat.retParam(1,200,null);
    }

    @Override
    public JsonBean select(String planname, String planyear, Integer pageNumber, Integer pageSize) {
        Page<TblTestTemplate> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNumber);
        LambdaQueryWrapper<TblTestplan> wrapper = Wrappers.<TblTestplan>lambdaQuery();
        if (planname!=null){
            wrapper.eq(TblTestplan::getPlanname,planname);
        }
        if (StringUtils.isNotBlank(planyear)){
            wrapper.eq(TblTestplan::getPlanyear,planyear);
        }
        tblTestplanMapper.selectPage(page,wrapper);
        return ResponseFormat.retParam(1,200,null);
    }
}
